/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gulangyu.trade.mvc;

import com.mycompany.yourtrade.common.UpYunService;
import com.mycompany.yourtrade.model.UserBean;
import com.mycompany.yourtrade.service.user.UserService;
import com.qunshang.common.SessionUtils;
import com.qunshang.common.UserBeanSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author xg
 */
@Controller
@RequestMapping("/manage")
public class AdminController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private UpYunService UpYunService;
    @Autowired
    private UserService userService;

    @RequestMapping(value = "login", method = RequestMethod.GET)
    public ModelAndView login(ModelMap model) {
        ModelAndView mv = new ModelAndView("login");
        logger.info("login page");
        return mv;
    }
    @RequestMapping(value = "index", method = RequestMethod.GET)
    public ModelAndView index(ModelMap model) {
        ModelAndView mv = new ModelAndView("manage/index");
        return mv;
    }

    @RequestMapping(value = "checklogin", method = RequestMethod.POST)
    public @ResponseBody
    String userlogin(HttpServletRequest request, HttpServletResponse response, String mobile, String pwd) {

        try {
            UserBean userBean = userService.getUserBeanByMobile(mobile);
            if (userBean != null) {
                if (userBean.getPassword().equals(pwd)) {
                    // 创建用户Session
                    UserBeanSession session = new UserBeanSession(userBean);
                    SessionUtils.setSession(request, session);
                    return "success";
                } else {
                    return "fail";
                }
            } else {
                return "fail";
            }
        } catch (Exception e) {
            logger.error(e.toString());
            return "fail";
        }
    }
}
